package com.example.Arithmetic.Arithmetic;

import java.util.Arrays;

/**
 * 日期：2023/12/26
 * 时间：22:18
 * 描述：选择排序
 */
public class SelectionSort {
    public void sort(int[] ints) {
        for (int right = ints.length - 1; right > 0; right--) {
            int max=right;
            for (int i = 0; i < right; i++) {
                if (ints[i] > ints[max]) {
                    max = i;
                }
            }
            if (max!=right){
                swap(ints,max,right);
            }
        }
    }
    private static void swap(int[] a, int i, int j) {
        int t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    public static void main(String[] args) {
        int[] a = {2, 3, 1, 7, 6, 4, 5};
        System.out.println(Arrays.toString(a));
        SelectionSort selectionSort=new SelectionSort();
        selectionSort.sort(a);
        System.out.println(Arrays.toString(a));
    }
}
